Java.perform(function() {
    console.log("====== Frida脚本已注入，正在监控AES参数 ======");

    // 1. Hook SecretKeySpec 获取密钥 (Key)
    var SecretKeySpec = Java.use('javax.crypto.spec.SecretKeySpec');
    SecretKeySpec.$init.overload('[B', 'java.lang.String').implementation = function(keyBytes, algorithm) {
        // 我们只关心AES算法的密钥
        if (algorithm.toUpperCase() === 'AES') {
            console.log("\n" + "="*20 + " 捕获到AES密钥! " + "="*20);
            console.log("算法: " + algorithm);
            
            // 将字节数组转换为十六进制字符串，方便查看和复制
            var keyHex = bytesToHex(keyBytes);
            console.log("密钥 (Hex): " + keyHex);
            console.log("密钥长度: " + keyBytes.length + " 字节");
            console.log("="*55 + "\n");
        }
        // 调用原始构造函数
        return this.$init.apply(this, arguments);
    };

    // 2. Hook IvParameterSpec 获取初始化向量 (IV)
    var IvParameterSpec = Java.use('javax.crypto.spec.IvParameterSpec');
    IvParameterSpec.$init.overload('[B').implementation = function(ivBytes) {
        console.log("\n" + "="*20 + " 捕获到IV! " + "="*20);
        
        // 将字节数组转换为十六进制字符串
        var ivHex = bytesToHex(ivBytes);
        console.log("IV (Hex): " + ivHex);
        console.log("IV长度: " + ivBytes.length + " 字节");
        console.log("="*50 + "\n");
        
        // 调用原始构造函数
        return this.$init.apply(this, arguments);
    };

    // 辅助函数：将字节数组转为十六进制字符串
    function bytesToHex(arr) {
        var hexStr = '';
        for (var i = 0; i < arr.length; i++) {
            var hex = (arr[i] & 0xFF).toString(16);
            hex = (hex.length === 1) ? '0' + hex : hex;
            hexStr += hex;
        }
        return hexStr;
    }
});